from pymongo import MongoClient
from pprint import pprint


def get_mongo_connection_status(connection_string: str):
    """
    获取 MongoDB 的连接状态信息
    """
    client = MongoClient(connection_string)
    try:
        # 执行 serverStatus 命令，只获取 connections 部分
        status = client.admin.command('serverStatus', connections=1)
        connections_info = status.get('connections', {})

        print("MongoDB 连接状态:")
        pprint(connections_info)

        return connections_info
    except Exception as e:
        print(f"获取连接状态失败: {e}")
    finally:
        client.close()


# 使用示例
if __name__ == "__main__":
    # 替换为你的连接字符串
    MONGO_CONNECTION_STRING = "mongodb://localhost:27017/"
    get_mongo_connection_status(MONGO_CONNECTION_STRING)
